<template>
  <div>
    <el-button
      type="primary"
      size="default"
      icon="el-icon-plus"
      style="margin-bottom: 10px"
      @click="dialogFormVisible = true"
      >添加</el-button
    >
    <el-table :data="shopList" border stripe>
      <el-table-column fixed label="序号" width="80px" align="center">
        <template v-slot="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="shopName"
        label="店名"
        width="200px"
      ></el-table-column>
      <el-table-column prop="address" label="详细地址"></el-table-column>
      <el-table-column
        prop="tel"
        label="电话联系方式"
        width="180px"
      ></el-table-column>
      <el-table-column label="操作" width="200px">
        <template slot-scope="{ row }">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="mini"
            @click="editData(row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteItem(row)"
          >
            删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="sForm.id ? '修改门店信息' : '添加门店'"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-form :model="sForm" ref="shopForm" style="width: 80%">
        <el-form-item label="门店名称" label-width="100px" prop="shopName">
          <el-input v-model="sForm.shopName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="省市选择" label-width="100px">
          <el-cascader
            size="large"
            :props="{ expandTrigger: 'hover' }"
            :options="provinceAndCityData"
            v-model="selectedOptions"
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item label="详细地址" label-width="100px" prop="address">
          <el-input v-model="sForm.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" label-width="100px" prop="tel">
          <el-input v-model.number="sForm.tel" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="cancelDialog">取消</el-button>
        <el-button type="primary" @click="submitData">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { provinceAndCityData, CodeToText } from "element-china-area-data";

export default {
  name: "Shop",
  data() {
    return {
      // 区域码转汉字
      CodeToText,
      // 数据源
      provinceAndCityData,
      shopList: [],
      // 是否显示表单
      dialogFormVisible: false,
      sForm: {
        shopName: "",
        address: "",
        tel: "",
      },
      //  选择的省份城市
      selectedOptions: ["110000", "110100"],
    };
  },
  mounted() {
    this.getShopList();
  },
  methods: {
    async getShopList() {
      let ret = await reqShopList();
      if (ret.code == 200) this.shopList = ret.data;
    },
    // 点击取消按钮
    cancelDialog() {
      this.sForm = {
        shopName: "",
        address: "",
        tel: "",
      };
      this.sForm.id = undefined;
      this.dialogFormVisible = false;
    },
    // 点击叉号关闭
    handleClose(done) {
      this.$confirm("确认关闭？", "提示", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((_) => {
          this.cancelDialog();
          done();
        })
        .catch((_) => {});
    },
    handleChange([province, city]) {
      // 区域码转汉字：{{ CodeToText[selectedOptions[0]] }},{{ CodeToText[selectedOptions[1]] }}</div>
      this.sForm.province = province;
      this.sForm.city = city;
    },
    editData(item) {
      this.dialogFormVisible = true;
      this.sForm = { ...item };
      this.selectedOptions = [item.province, item.city];
    },
    async submitData() {
      let ret = await reqAddOrUpdateShop(this.sForm);
      let tip = this.sForm._id ? "门店信息修改成功" : "门店添加成功";
      if (ret.code == 200) {
        this.$message.success(tip);
        this.getShopList();
        this.cancelDialog();
      }
    },
    deleteItem(row) {
      this.$confirm(`你确定删除店名为${row.shopName}?`, "提示", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          //当用户点击确定按钮的时候会出发
          //向服务器发请求
          let result = await reqDeleteShop({ id: row._id });
          //如果删除成功
          if (result.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getShopList();
          }
        })
        .catch(() => {
          //当用户点击取消按钮的时候会触发
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style></style>
